#include "HookLibrary.h"
#include "HookInfoFile.h"
#include "EventHandler.h"

#define BG_MOD_SHIFT   (0x10000)
#define BG_MOD_CONTROL (0x20000)
#define BG_MOD_ALT     (0x40000)

static volatile UINT BGShellHookMessage = 0;

UINT WINAPI HookMessage()
{
	if (BGShellHookMessage == 0)
	{
		BGShellHookMessage = RegisterWindowMessageW(L"WM_BGShell_KeyboardHook");
	}

	return BGShellHookMessage;
}

DWORD WINAPI GetWinFormsKey(UINT Vk, BOOL Control, BOOL Shift, BOOL Alt)
{
	DWORD Result = Vk;

	if ((Vk != VK_LCONTROL) && (Vk != VK_RCONTROL))
	{
		Result |= (Control ? BG_MOD_CONTROL : 0);
	}

	if ((Vk != VK_LSHIFT) && (Vk != VK_RSHIFT))
	{
		Result |= (Shift ? BG_MOD_SHIFT : 0);
	}

	if ((Vk != VK_LMENU) && (Vk != VK_RMENU))
	{
		Result |= (Alt ? BG_MOD_ALT : 0);
	}

	return Result;
}

BOOL WINAPI IsKeyDownEventRegistered(DWORD vk)
{
	LPDWORD Keys = HookInfo->Keys;
	SIZE_T  Count = HookInfo->KeyCount;
	SIZE_T  i;

	for (i = 0; i < Count; i++)
	{
		if (Keys[i] == vk)
		{
			return TRUE;
		}
	}

	return FALSE;
}

VOID WINAPI PostKeyDownEvent(HWND hWnd, DWORD vk)
{
	PostMessageW(hWnd, HookMessage(), 0, vk);
}